var {
  wechatLogin,
  queryAreaByPage,
  queryEventTypeByPage,
  queryEventLevelByPage
} = require('./utils/api');
const updateManager = wx.getUpdateManager()
App({
  onLaunch() {
    let _this = this;
    
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function () {
          wx.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
              // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        })
        updateManager.onUpdateFailed(function () {
          // 新的版本下载失败
          wx.showModal({
            title: '已经有新版本了哟~',
            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
          })
        })
      }
    })

    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        wechatLogin(res).then((result) => {
          console.log('注册返回==', result)
          _this.globalData.userInfo = result.result
          _this.globalData.id = result.result ? result.result.id : '';
          _this.globalData.token = result.result ? result.result.token : '';
          _this.globalData.mobile = result.result ? result.result.mobile : '';
          _this.globalData.openid = JSON.parse(result.data).openid; //okZJ267V5SPOoDqHxQ0yyXlzjh_A
          console.log('注册返回=_this.globalData.mobile=', _this.globalData.mobile)


          //页面加载时获取村居列表信息
          queryAreaByPage(1, 200).then(res => {
            let ary = res[1].content;
            let newAry = ary.map((item) => {
              return {
                value: item.id,
                label: item.areaName
              }
            })
            _this.globalData.areaArray = newAry;
          })
          //页面加载时事件类型 家庭类，环保类，违章违建类....
          queryEventTypeByPage(1, 200).then(res => {
            let ary = res[1].content;
            let newAry = ary.map((item) => {
              return {
                value: item.id,
                label: item.event_type
              }
            })
            _this.globalData.typeArray = newAry;
          })
          //页面加载时事件等级-   一般，严重，特重
          queryEventLevelByPage(1, 200).then(res => {
            let ary = res[1].content;
            let newAry = ary.map((item) => {
              return {
                value: item.id,
                label: item.event_level
              }
            })
            _this.globalData.levelArray = newAry;
          })



          if (result.result) { //如果有用户信息，直接根据角色条跳转到对应的业务页面
            if (result.result.roles == 4) { //跳转个人
              wx.redirectTo({
                url: '/pages/personal/index'
              })
            } else if (result.result.roles == 2) { //跳转到工作人员
              wx.redirectTo({
                url: '/pages/workerhome/index'
              })
            } else if (result.result.roles == 3) { //跳转到管理员
              wx.redirectTo({
                url: '/pages/managehome/index'
              })
            }
          } else {

          }
        });
      }
    })
  },
  //全局变量
  globalData: {
    userInfo: '',
    token: '',
    id: '',
    token: '',
    unionid: '',
    openid: '',
    mobile: '',
    levelArray: [],
    typeArray: [],
    areaArray: [],
    indexTabBarLists: [{
        "text": "业务办理",
        "iconPath": "/images/home1.png",
        "selectedIconPath": "/images/home.png"
      },
      {
        "text": "我的事件",
        "iconPath": "/images/wo1.png",
        "selectedIconPath": "/images/wo.png"
      },
      {
        "text": "个人中心",
        "iconPath": "/images/info1.png",
        "selectedIconPath": "/images/info.png"
      }
    ],
    listzu: [{
        "text": "首页",
        "iconPath": "/images/work.png",
        "selectedIconPath": "/images/workhome.png"
      },
      {
        "text": "个人中心",
        "iconPath": "/images/workif.png",
        "selectedIconPath": "/images/workinfo.png"
      }
    ],
    listgl: [{
        "text": "预警",
        // "iconPath": "/images/home1.png",
        // "selectedIconPath": "/images/home.png"
      },
      {
        "text": "查看",
        // "iconPath": "/images/home1.png",
        // "selectedIconPath": "/images/home.png"
      },
      {
        "text": "数据统计",
        // "iconPath": "/images/home1.png",
        // "selectedIconPath": "/images/home.png"
      }
    ],

    tabChange: function (e) {
      wx.redirectTo({
        url: e.detail.item.pagePath
      })
    }
  }

})